package com.sdx.console.system.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Collections;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sdx.common.utils.JacksonUtils;
import com.sdx.common.entity.DictBusiness;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.web.JsonResult;
import com.ibeetl.admin.core.util.*;
import com.sdx.console.system.service.*;
import com.sdx.console.system.web.query.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import com.ibeetl.admin.core.web.AuthorizedBaesController;

/**
 * 业务字典 接口
 */
@RequestMapping("/system/dictBusiness")
@Controller
public class DictBusinessController extends AuthorizedBaesController{
	private static final Logger log = LoggerFactory.getLogger(DictBusinessController.class);
    private static final String MODEL = "/system/dictBusiness";


    @Resource
	private DictBusinessService dictBusinessService;
    
    @Resource
    FileService fileService;
    /* 页面 */

    @GetMapping("/index.do")
    @Function("system.dictBusiness.query")
    @ResponseBody
    public ModelAndView index() {
        ModelAndView view = new ModelAndView(MODEL+"/index.html") ;
        return view;
    }

    @GetMapping("/edit.do")
    @Function("system.dictBusiness.edit")
    @ResponseBody
    public ModelAndView edit(Integer dictId) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        DictBusiness dictBusiness = dictBusinessService.queryById(dictId);
        view.addObject("dictBusiness", dictBusiness);
		try {
			view.addObject("dictBusinessJson", JacksonUtils.toJsonString(dictBusiness,JacksonUtils.DF1));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
        return view;
    }

    @GetMapping("/add.do")
    @Function("system.dictBusiness.add")
    @ResponseBody
    public ModelAndView add() {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        return view;
    }

    /* ajax json */

    @PostMapping("/list.json")
    @Function("system.dictBusiness.query")
    @ResponseBody
    public JsonResult<PageQuery> list(DictBusinessQuery condtion)
    {
        PageQuery page = condtion.getPageQuery();
        dictBusinessService.queryByCondition(page);
        return JsonResult.success(page);
    }

    private JsonResult save(boolean isNew,DictBusiness dictBusiness,HttpServletRequest request
	) {
    	JsonResult result = JsonResult.success();
		String dir = DateUtil.now("yyyyMMdd");
    	String subPath = "/images/cover/"+dir+"/";
		CoreUser user = getLoginUser(request);

    	if (isNew) {
			dictBusiness.setCreatedBy(user.getId().intValue());
			dictBusiness.setCreatedName(user.getName());
			dictBusiness.setCreatedTime(new Date());
			dictBusinessService.save(dictBusiness);
		} else {
			dictBusiness.setUpdatedBy(user.getId().intValue());
			dictBusiness.setUpdatedName(user.getName());
			dictBusiness.setUpdatedTime(new Date());
			dictBusinessService.updateTemplate(dictBusiness);
		}
		return result;
	}
    @PostMapping("/add.json")
    @Function("system.dictBusiness.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)DictBusiness dictBusiness,HttpServletRequest request
		)
    {

        return save(true,dictBusiness,request
		);
    }

    @PostMapping("/update.json")
    @Function("system.dictBusiness.edit")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  DictBusiness dictBusiness,HttpServletRequest request
		) {
		return save(false,dictBusiness,request
		);
    }


   
    @GetMapping("/view.json")
    @Function("system.dictBusiness.query")
    @ResponseBody
    public JsonResult<DictBusiness>queryInfo(Integer dictId) {
        DictBusiness dictBusiness = dictBusinessService.queryById( dictId);
        return  JsonResult.success(dictBusiness);
    }

    @PostMapping("/delete.json")
    @Function("system.dictBusiness..del")
    @ResponseBody
    public JsonResult delete(String ids) {
        if (ids.endsWith(",")) {
            ids = StringUtils.substringBeforeLast(ids, ",");
        }
        //List<Long> idList = ConvertUtil.str2longs(ids);
		List<String> idList = new ArrayList<String>();
		Collections.addAll(idList,ids.split(","));
        dictBusinessService.batchDelDictBusiness(idList);
        return JsonResult.success();
    }
    

}
